/**
 * 2006-12-8 17:19:30
 * RegisterForm.java
 */
package org.lilystudio.veryftp.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 * @author Samson
 * @version 0.1
 */
public class RegisterForm extends ActionForm {

	private static final long serialVersionUID = 1L;

	private String step;

	// for step2
	private String ftpAddr;

	private int ftpPort;

	private String ftpUserName;

	private String ftpUserPass;

	private String ftpDownloadUserName;

	private String ftpDownloadUserPass;

	// for step3
	private String ftperUserName;

	private String ftperUserPass;

	private String ftperUserConfirmPass;

	private String ftperEmail;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping,
	 *      javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		step = null;

		ftpAddr = null;
		ftpPort = 21;
		ftpUserName = null;
		ftpUserPass = null;
		ftpDownloadUserName = null;
		ftpDownloadUserPass = null;

		ftperUserName = null;
		ftperUserPass = null;
		ftperUserConfirmPass = null;
		ftperEmail = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
	 *      javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		if (step == null || step.length() < 1) {
			step = "step1";
		}
		if (step.equals("step2")) {
			if (ftpAddr == null || ftpAddr.length() < 1) {
				// the error processing move to RegisterAciton...
				ftpAddr = "";
			}
			if (ftpPort < 0) {
				ftpPort = 21;
			}
			if (ftpUserName == null || ftpUserName.length() < 1) {
				ftpUserName = "anonymous";
			}
			if (ftpUserPass == null || ftpUserPass.length() < 1) {
				ftpUserPass = "";
			}
			if (request.getParameter("sameWithList") != null) {
				ftpDownloadUserName = ftpUserName;
				ftpDownloadUserPass = ftpUserPass;
			} else {
				if (ftpDownloadUserName == null
						|| ftpDownloadUserName.length() < 1) {
					ftpDownloadUserName = ftpUserName;
				}
				if (ftpDownloadUserPass == null
						|| ftpDownloadUserPass.length() < 1) {
					ftpDownloadUserPass = ftpUserPass;
				}
			}
		} else if (step.equals("step3")) {
			boolean hasError = false;
			if (ftperUserName == null || ftperUserName.length() < 1) {
				errors.add("ftperUserName", new ActionMessage(
						"veryftp.error.ftper.userName.required"));
				hasError = true;
			}
			if (ftperUserPass == null || ftperUserPass.length() < 1) {
				errors.add("ftperUserPass", new ActionMessage(
						"veryftp.error.ftper.userPass.required"));
				ftperUserPass = "";
				hasError = true;
			}
			if (ftperUserConfirmPass == null
					|| ftperUserConfirmPass.length() < 1) {
				errors.add("ftperUserConfirmPass", new ActionMessage(
						"veryftp.error.ftper.userConfirmPass.required"));
				ftperUserConfirmPass = "";
				hasError = true;
			}
			if (!ftperUserPass.equals(ftperUserConfirmPass)) {
				errors.add("ftperUserPass", new ActionMessage(
						"veryftp.error.ftper.userPass.unequal"));
				hasError = true;
			}
			if (ftperEmail == null || ftperEmail.length() < 1) {
				errors.add("ftperEmail", new ActionMessage(
						"veryftp.error.ftper.email.required"));
				hasError = true;
			}
			// if have error, still remain in step2
			if (hasError) {
				mapping.findForward("step2");
			}
		}

		return errors;
	}

	/**
	 * @return Returns the ftpAddr.
	 */
	public String getFtpAddr() {
		return ftpAddr;
	}

	/**
	 * @param ftpAddr
	 *            The ftpAddr to set.
	 */
	public void setFtpAddr(String ftpAddr) {
		this.ftpAddr = ftpAddr;
	}

	/**
	 * @return Returns the ftpUserName.
	 */
	public String getFtpUserName() {
		return ftpUserName;
	}

	/**
	 * @param ftpUserName
	 *            The ftpUserName to set.
	 */
	public void setFtpUserName(String ftpUserName) {
		this.ftpUserName = ftpUserName;
	}

	/**
	 * @return Returns the ftpUserPass.
	 */
	public String getFtpUserPass() {
		return ftpUserPass;
	}

	/**
	 * @param ftpUserPass
	 *            The ftpUserPass to set.
	 */
	public void setFtpUserPass(String ftpUserPass) {
		this.ftpUserPass = ftpUserPass;
	}

	/**
	 * @return Returns the step.
	 */
	public String getStep() {
		return step;
	}

	/**
	 * @param step
	 *            The step to set.
	 */
	public void setStep(String step) {
		this.step = step;
	}

	/**
	 * @return Returns the ftperEmail.
	 */
	public String getFtperEmail() {
		return ftperEmail;
	}

	/**
	 * @param ftperEmail
	 *            The ftperEmail to set.
	 */
	public void setFtperEmail(String ftperEmail) {
		this.ftperEmail = ftperEmail;
	}

	/**
	 * @return Returns the ftperUserConfirmPass.
	 */
	public String getFtperUserConfirmPass() {
		return ftperUserConfirmPass;
	}

	/**
	 * @param ftperUserConfirmPass
	 *            The ftperUserConfirmPass to set.
	 */
	public void setFtperUserConfirmPass(String ftperUserConfirmPass) {
		this.ftperUserConfirmPass = ftperUserConfirmPass;
	}

	/**
	 * @return Returns the ftperUserName.
	 */
	public String getFtperUserName() {
		return ftperUserName;
	}

	/**
	 * @param ftperUserName
	 *            The ftperUserName to set.
	 */
	public void setFtperUserName(String ftperUserName) {
		this.ftperUserName = ftperUserName;
	}

	/**
	 * @return Returns the ftperUserPass.
	 */
	public String getFtperUserPass() {
		return ftperUserPass;
	}

	/**
	 * @param ftperUserPass
	 *            The ftperUserPass to set.
	 */
	public void setFtperUserPass(String ftperUserPass) {
		this.ftperUserPass = ftperUserPass;
	}

	/**
	 * @return Returns the ftpPort.
	 */
	public int getFtpPort() {
		return ftpPort;
	}

	/**
	 * @param ftpPort
	 *            The ftpPort to set.
	 */
	public void setFtpPort(int ftpPort) {
		this.ftpPort = ftpPort;
	}

	/**
	 * @return Returns the ftpDownloadUserName.
	 */
	public String getFtpDownloadUserName() {
		return ftpDownloadUserName;
	}

	/**
	 * @param ftpDownloadUserName
	 *            The ftpDownloadUserName to set.
	 */
	public void setFtpDownloadUserName(String ftpDownloadUserName) {
		this.ftpDownloadUserName = ftpDownloadUserName;
	}

	/**
	 * @return Returns the ftpDownloadUserPass.
	 */
	public String getFtpDownloadUserPass() {
		return ftpDownloadUserPass;
	}

	/**
	 * @param ftpDownloadUserPass
	 *            The ftpDownloadUserPass to set.
	 */
	public void setFtpDownloadUserPass(String ftpDownloadUserPass) {
		this.ftpDownloadUserPass = ftpDownloadUserPass;
	}

}
